package com.sf.iisp.bind.dao.impl;

import com.sf.iisp.bind.dao.IUnbindingDao;
import com.sf.iisp.bind.domain.UnbindingRecord;
import com.sf.novatar.base.dao.BaseBeanDao;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * Created by qi on 16-7-21.
 */
@Repository
public class UnbindingDao extends BaseBeanDao<Long, UnbindingRecord> implements IUnbindingDao {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateUnbindRecord(UnbindingRecord br) {
        saveOrUpdate(br);

    }

    @Override
    public UnbindingRecord findByStateAndPhones(String state, String courierPhone, String customerPhone) {
        String sql = "select  *  from unbind_record  b where  b.state=:state and b.courier_phone =:courierPhone  and b.customer_phone =:customerPhone";
        SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
        query.setString("state", state);
        query.setString("customerPhone", customerPhone);
        query.setString("courierPhone", courierPhone);
//        query.setString("virtualNo", virtualNo);
        query.addEntity(UnbindingRecord.class);
        @SuppressWarnings("unchecked")
        ArrayList<UnbindingRecord> list = (ArrayList<UnbindingRecord>) query.list();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
